package ru.ssau.berlin.jsf.converters;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import ru.ssau.berlin.ejb.facade.GroupFacade;
import ru.ssau.berlin.ejb.facade.GroupFacadeLocal;
import ru.ssau.berlin.entity.GroupType;
import ru.ssau.berlin.framework.JNDIHelper;


public class GroupTypeConverter implements Converter {

    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        InitialContext ic;
        GroupType type = null;
        try {
            ic = new InitialContext();
            GroupFacadeLocal facade =
                    (GroupFacadeLocal) ic.lookup(JNDIHelper.getBeanName(GroupFacade.class));
            type = facade.findGroupType(Long.parseLong(value));
        } catch (NamingException ex) {
            Logger.getLogger(PersonTypeConverter.class.getName()).log(Level.SEVERE, null, ex);
        }
        return type;
    }

    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return ((GroupType) value).getId().toString();
    }

}
